<template>
  <div class="welfare">
    <p class="detail-tip">
      公司福利<b v-if="!isStatic">（可选五项）</b><span v-if="showView && !isStatic" @click="addList">添加</span>
    </p>
    <div class="com-container" v-if="showView">
      <div class="show-item">
        <span class="name" v-for="v in list" :key="v.id">{{ v.tagname }}</span>
      </div>
      <p class="btn-wrap" v-if="!isStatic && list.length !== 0">
        <span class="edit" @click="toEdit">编辑</span>
      </p>
    </div>
    <div class="com-form" v-else>
      <div class="postForm">
        <div class="showEdit" v-if="showDelete">
          <div
            class="form-item"
            v-for="(m, n) in list"
            :key="'editWelfare_' + n"
          >
            <span class="name">{{ m.tagname }}</span>
            <span class="delete" @click="toDelete(n)">删除</span>
          </div>
        </div>
        <div class="form-select" v-else>
          <div class="form-item">
            <div class="text" required>选择福利:</div>
            <div class="select">
              <client-only>
                <el-select
                  v-model="edit.tagname"
                  placeholder="请选择福利"
                  @change="changeId"
                >
                  <el-option
                    v-for="item in chooseList"
                    :key="'chooseList_' + item.id"
                    :label="item.tagname"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </client-only>
            </div>
          </div>
        </div>
        <div class="submit">
          <button class="confirm" type="button" @click="submit">保存</button>
          <button class="cancel" type="button" @click="cancel">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ['welfareList', 'welfares', 'isStatic'],
  data () {
    return {
      showView: true,
      showDelete: false,
      list: [],
      edit: { id: '', tagname: '' },
      chooseList: [],
    }
  },
  watch: {
    welfareList (val) {
      this.list = val;
    },
    welfares (val) {
      this.chooseList = val;
    },
  },
  created () {
    this.list = this.welfareList || [];
    this.chooseList = this.welfares || [];
  },
  mounted () {
  },
  methods: {
    changeId (data) {
      this.edit.id = data;
      const curOne = this.chooseList.find(s => s.id === data) || {};
      this.edit.tagname = curOne.tagname || '';
    },
    addList () {
      if (this.list.length >= 5) {
        this.$message.error('最多添加5个！');
        return
      }
      this.showView = false;
    },
    toggleView () {
      this.showView = false;
    },
    toEdit () {
      this.showDelete = true;
      this.showView = false;
    },
    toDelete (index) {
      this.list.splice(index, 1);
    },
    cancel () {
      if (this.showDelete) {
        this.showDelete = false;
      }
      this.showView = true;
    },
    submit () {
      if (this.showDelete) { // 编辑
        this.showDelete = false;
      } else { // 添加
        this.list.push(this.edit);
        this.edit = { id: '', tagname: '' };
      }
      this.showView = true;
    },
  }
}
</script>
<style lang="scss" scoped>
.welfare {
  position: relative;
  .detail-tip {
    b{
      font-weight: 500;
      margin-left: 5px;
      color: #999;
      font-size: 14px;
    }
  }
  .show-item {
    line-height: 40px;
    padding-left: 30px;
    span {
      line-height: 22px;
      display: inline-block;
      padding: 0 15px;
      margin: 0 20px 20px 0;
      border: 1px solid #999;
      border-radius: 4px;
      font-size: 14px;
      color: #666;
    }
  }
  .btn-wrap {
    position: absolute;
    top: 55px;
    right: 20px;
    span {
      margin-left: 20px;
    }
    .edit {
      color: $base-color;
      cursor: pointer;
      &:hover {
        color: $base-color-hover;
      }
    }
  }
  .com-form {
    padding: 30px 0;
    .postForm {
      width: 500px;
      margin: 0 auto;
      .showEdit {
        padding: 0 20px 0 50px;
        .form-item {
          min-height: 40px;
          line-height: 40px;
          .delete {
            float: right;
            color: $base-color-danger;
            &:hover {
              color: $base-color-important;
              cursor: pointer;
            }
          }
        }
      }
      .form-item {
        margin-bottom: 10px;
        line-height: 40px;
        & > div {
          display: inline-block;
        }
        & > .text {
          position: relative;
          padding-left: 20px;
          min-width: 120px;
          color: #24292f;
          vertical-align: top;
          &[required] {
            &::before {
              content: '*';
              position: absolute;
              left: 0px;
              top: 0px;
              color: $base-color-danger;
            }
          }
        }
        .input {
          input {
            line-height: 28px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 0 10px;
            width: 350px;
            font-size: 16px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
        }
        label {
          margin-right: 30px;
          cursor: pointer;
        }
        .select {
          .el-input {
            width: 100%;
          }
          ::v-deep .el-input__inner {
            width: 350px;
            height: 30px;
            line-height: 30px;
            border: 1px solid #ccc;
            font-size: 16px;
            color: #24292f;
            padding-right: 10px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
          &.small {
            .el-input {
              width: 150px;
              display: inline-block;
            }
            ::v-deep .el-input__inner {
              width: 153px;
              padding-right: 20px;
              margin-right: 20px;
            }
            ::v-deep .el-input__suffix {
              right: 20px;
            }
          }
        }
      }
    }
  }
}
</style>
